package com.chenglu.android.common.utils;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;

import com.yuruiyin.appbarlayoutbehavior.LogUtil;

import java.util.List;

/**
 * @author: lyj
 * @Created: by Copyright 2023 3 Guangzhou Hongma Technology Co., Ltd. All rights reserved.
 * @Description：
 */
public class AppCheckInstall {



        public static boolean checkApkExist(Context context, String packageName) {
            if (packageName == null || "".equals(packageName))
                return false;
            try {
                ApplicationInfo info = context.getPackageManager().getApplicationInfo(packageName,
                        PackageManager.GET_UNINSTALLED_PACKAGES);
                return true;
            } catch (PackageManager.NameNotFoundException e) {
                return false;
            }
        }

        /**
         * 检测是否安装支付宝
         * @return     * @param context
         */
        public static boolean checkAliPayInstalled(Context context){
            Uri uri = Uri.parse("alipays://platformapi/startApp");
            Intent intent = new Intent(Intent.ACTION_VIEW,uri);
            ComponentName componentName = intent.resolveActivity(context.getPackageManager());
            return componentName!=null;

        }

        /**
         * 判断 用户是否安装微信客户端
         */
        public static  boolean isWeixinAvilible(Context context){
            final PackageManager packageManager = context.getPackageManager(); //获取packagemanager
            List<PackageInfo> pinfo =packageManager.getInstalledPackages(0);// 获取所有已安装程序的包信息
            if (pinfo !=null){
                for (int i = 0; i < pinfo.size(); i++) {
                    String pn = pinfo.get(i).packageName;
                    if (pn.equals("com.tencent.mm")){
                        return true;
                    }
                }
            }
            return false;
        }


        /**
         * 判断 用户是否安装QQ客户端
         */
        public static boolean isQQClientAvaolable(Context context){
            final PackageManager packageManager = context.getPackageManager();
            List<PackageInfo> pinfo = packageManager.getInstalledPackages(0);
            if (pinfo !=null){
                for (int i = 0; i < pinfo.size(); i++) {
                    String pn = pinfo.get(i).packageName;
                    if (pn.equalsIgnoreCase("com.tencent.qqlite")||pn.equalsIgnoreCase("com.tencent.mobileqq")){
                        return true;
                    }
                }
            }
            return false;
        }

    /**
     * 判断 用户是否安装SY客户端
     */
    public static  boolean isSYAvilible(Context context){
        final PackageManager packageManager = context.getPackageManager(); //获取packagemanager
        List<PackageInfo> pinfo =packageManager.getInstalledPackages(0);// 获取所有已安装程序的包信息
        if (pinfo !=null){
            for (int i = 0; i < pinfo.size(); i++) {
                String pn = pinfo.get(i).packageName;
//                LogUtils.d("包名"+ pinfo.get(i).packageName);
                if (pn.equals("com.symall.android")){

                    return true;
                }
            }
        }
        return false;
    }
}
